﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.QuanLyKho
{
    public partial class CapNhatLenh : Form
    {
        public static string MaID = "0";
        public static string IdLenh = "0";
        public static bool xem = false;
        public string IdSanPham = "";
        public string MaSanPham = "";

        public CapNhatLenh()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
        }

        public void Loaddata()
        {
            if (MaID == "0" && IdLenh != "0")
            {
                LoadGrid();
                SetPropertiesColumns();
                DataTable dt = DataAll.SelectAll("LenhSanXuat", "*", "WHERE ID = " + IdLenh);
                dtNgayLapLenh.Value = Convert.ToDateTime(dt.Rows[0]["NgayLapLenh"]);
                txtSoLuong.Text = dt.Rows[0]["SoLuong"].ToString();
                txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(dt.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(dt.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = dt.Rows[0]["SoChungTu"].ToString();
                dtSXTuNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXTuNgay"]);
                dtSXDenNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXDenNgay"]);
                IdSanPham = dt.Rows[0]["IdSanPham"].ToString();
                var data2 = DataAll.SelectAll(
                    "LenhSanXuat_ChiTiet ct LEFT JOIN Kho_VatTu vt ON ct.MaVatTu = vt.MaVatTu",
                    "ct.ID,[IDLenhSanXuat],ct.[MaVatTu],vt.MaVatTu as'Mã vật tư',[S],[M],[L],[XL]," +
                    "[TongCan] as'Tổng cần', [DMTT] as'ĐMTT',[SoLuongCan] as'SL cần'," +
                    "[DauTam] as'Đầu tấm',ct.[MoTa] as'Mô tả',ct.[GhiChu] as'Ghi chú', ct.[LoaiVatTu] as 'Loại vật tư'",
                    " WHERE IDLenhSanXuat =" + IdLenh);
                gridChiTiet.DataSource = null;
                gridChiTiet.DataSource = data2;
            }
            LoadThongTinChung();
            LoadImage();
        }

        public void LoadThongTinChung()
        {
            var data = DataAll.SelectAll("PhongMau_SanPham", "*", " WHERE IdSanPham=N'" + IdSanPham + "'");
            txtMaSP.Text = data.Rows[0]["MaHang"].ToString();
            txtLoaiHang.Text = data.Rows[0]["LoaiHang"].ToString();
            txtLanTai.Text = data.Rows[0]["LanSanXuat"].ToString();
            txtSoLenh.Text = data.Rows[0]["LenhSanXuat"].ToString();
            MaSanPham = data.Rows[0]["MaHang"].ToString();
        }

        public void SetPropertiesGrid()
        {
            gvChiTiet.OptionsView.ShowGroupPanel = false;
            gvChiTiet.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            gvChiTiet.OptionsView.ShowFooter = true;
            gvChiTiet.OptionsBehavior.Editable = false;
            gvChiTiet.OptionsBehavior.Editable = false;
            for (int i = 0; i < 9; i++)
            {
                gvChiTiet.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
            }
        }

        public void SetPropertiesColumns()
        {
            //Ma hang
            luMaVatTu.DataSource = DataAll.SelectAll("Kho_VatTuDuKien", "IDVatTuDuKien,MaVatTu,TenVatTu", "");
            luMaVatTu.Columns.Add(new LookUpColumnInfo("IDVatTuDuKien"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("MaVatTu"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("TenVatTu"));
            luMaVatTu.DisplayMember = "TenVatTu";
            luMaVatTu.ValueMember = "MaVatTu";
            luMaVatTu.NullText = string.Empty;
            luMaVatTu.Columns["IDVatTuDuKien"].Visible = false;

            // Loại vật tư
            DataTable dtLoaiVatTu = new DataTable("dsSiNO");
            dtLoaiVatTu.Columns.Add("LoaiVatTu", typeof(string));
            dtLoaiVatTu.Columns.Add("Value", typeof(int));
            dtLoaiVatTu.Rows.Add(Common.HienThiLoaiVatTu(Convert.ToInt32(LoaiVatTu.CatMay)), Convert.ToInt32(LoaiVatTu.CatMay));
            dtLoaiVatTu.Rows.Add(Common.HienThiLoaiVatTu(Convert.ToInt32(LoaiVatTu.HoanThien)), Convert.ToInt32(LoaiVatTu.HoanThien));

            luLoaiVatTu.Properties.DataSource = dtLoaiVatTu;
            luLoaiVatTu.Properties.Columns.Add(new LookUpColumnInfo("Value"));
            luLoaiVatTu.Properties.Columns.Add(new LookUpColumnInfo("LoaiVatTu"));
            luLoaiVatTu.Properties.DisplayMember = "LoaiVatTu";
            luLoaiVatTu.Properties.ValueMember = "Value";
            luLoaiVatTu.Properties.Columns["Value"].Visible = false;
            luLoaiVatTu.Properties.NullText = string.Empty;
            luLoaiVatTu.Properties.ShowHeader = false;

            //Format columns

            gcM.DisplayFormat.FormatType = FormatType.Custom;
            gcM.DisplayFormat.FormatString = "{0:0,0.00}";
            gcS.DisplayFormat.FormatType = FormatType.Numeric;
            gcS.DisplayFormat.FormatString = "{0:0,0.00}";
            gcL.DisplayFormat.FormatType = FormatType.Numeric;
            gcL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcXL.DisplayFormat.FormatType = FormatType.Numeric;
            gcXL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcDMTT.DisplayFormat.FormatType = FormatType.Numeric;
            gcDMTT.DisplayFormat.FormatString = "{0:0,0.00}";
            gcTongCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcTongCan.DisplayFormat.FormatString = "{0:0,0.00}";
            gcSLCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcSLCan.DisplayFormat.FormatString = "{0:0,0.00}";

            gcTongCan.OptionsColumn.AllowEdit = false;
            //gcXoa.Width = 20;
            //gcXoa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcS.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDauTam.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;

            //bntLuLai.Visible = xem;
            //if (!xem)
            //{
            //    gvChiTiet.OptionsBehavior.Editable = false;
            //}
        }

        public void LoadGrid()
        {
            var dataColumns = new DataTable();
            dataColumns.Columns.Add("Mã vật tư");
            dataColumns.Columns.Add("Mô tả");
            dataColumns.Columns.Add("S", typeof(double));
            dataColumns.Columns.Add("M", typeof(double));
            dataColumns.Columns.Add("L", typeof(double));
            dataColumns.Columns.Add("XL", typeof(double));
            dataColumns.Columns.Add("Tổng cần", typeof(double));
            dataColumns.Columns.Add("ĐMTT", typeof(double));
            dataColumns.Columns.Add("SL cần", typeof(double));
            dataColumns.Columns.Add("Đầu tấm");
            dataColumns.Columns.Add("Ghi chú");
            dataColumns.Columns.Add("Loại vật tư");

            pbAnh.Image = null;
            for (int i = 0; i < 1; i++)
            {
                dataColumns.Rows.Add("");
            }
            gridChiTiet.DataSource = null;
            gridChiTiet.DataSource = dataColumns;
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + MaSanPham + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bntLuLai_Click(object sender, EventArgs e)
        {
            string message = string.Empty;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                var soluong = DataAll.SelectAll("Kho_VatTu", "SoLuong"," WHERE MaVatTu=N'" + gvChiTiet.GetDataRow(i)["MaVatTu"] + "'");
                message = DataAll.Update("Kho_VatTu", "SoLuong = " + Convert.ToString(Convert.ToInt32(soluong.Rows[0]["SoLuong"]) - Convert.ToInt32(gvChiTiet.GetDataRow(i)["SL cần"])) + " WHERE MaVatTu=N'" + gvChiTiet.GetDataRow(i)["MaVatTu"] + "'");
                if (message != "success")
                {
                    break;
                }
            }
            message = DataAll.Update("LenhSanXuat", "TrangThai =" + Convert.ToInt32(TrangThai.LenhSanXuat_CapNhatKho) + " WHERE ID =" + IdLenh);
            MessageBox.Show(message.Equals("success") ? "Cập nhật thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            XemAnh.MaID = txtMaSP.Text;
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }
    }
}
